package com.novel.app.view.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.dongdxiang.framework.BaseActivity;
import com.dongdxiang.framework.dialog.Snackbar;
import com.dongdxiang.framework.http.QjResult;
import com.google.gson.reflect.TypeToken;
import com.novel.app.R;
import com.novel.app.utils.CommUtils;
import com.novel.app.utils.Configs;
import com.novel.app.view.main.adpter.Adpter;
import com.novel.app.view.main.bean.BookracksEntity;
import com.novel.app.view.shucheng.ShuChengActivity;
import com.novel.app.view.storys.StoryActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class MainActivity extends BaseActivity {
    private Adpter adpter;
    private ListView listView;
    private boolean isEdit = false;
    private View no_novel;
    private TextView delete;
    private boolean isAll = false;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_main);
        setCenterTitle(R.string.shucheng);
        setRight_Btn(R.string.shujia,onClickListener);
        adpter = new Adpter(this,listener);
        listView = (ListView) findViewById(R.id.listview);
        no_novel = findViewById(R.id.no_novel);
        delete = (TextView)findViewById(R.id.delete);
        doNet();
    }

    private void doNet(){
        String url = Configs.Bookracks;
        httpGetRequest(0,url,new TypeToken<QjResult<List<BookracksEntity>>>() {},true,true);
    }

    private void sin_in(){
        String url = Configs.user_sign;
        httpGetRequest(1,url,new TypeToken<QjResult<Object>>() {},true,true);
    }

    private void doDelete(){

        String ids = adpter.getCheckedIds();
        if (ids != null){
            String url = Configs.deleteBookrack+"&ids=";
            httpDeleteRequest(0,url,new TypeToken<QjResult<Object>>() {},false,true);
        }
    }
    @Override
    protected void onSucceed(int what, Object response) {
        if (response != null){
            if (what == 0){
                List<BookracksEntity> list = (List<BookracksEntity>)response;
                if (list != null && list.size() > 0){
                    adpter.addAll(list);
                    listView.setAdapter(adpter);
                    no_novel.setVisibility(View.GONE);
                    setLeft_Btn(R.string.bianji,onClickListener);
                }else no_novel.setVisibility(View.VISIBLE);
            }else{
                Snackbar.show(this,"签到成功！");
            }
        }
    }

    @Override
    protected void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {

    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_left:
                    if (adpter != null && adpter.getCount() > 0){
                        adpter.isEdit(isEdit = !isEdit);
                        if (isEdit) {
                            setLeft_Btn("完成");
                            setCenterTitle("编辑书城");
                            setRight_Btn("全选",0);
                            delete.setVisibility(View.VISIBLE);
                        }else {
                            setCenterTitle(R.string.shucheng);
                            setLeft_Btn("编辑");
                            setRight_Btn("书架",0);
                            delete.setVisibility(View.GONE);
                        }
                        adpter.notifyDataSetChanged();
                    }
                    break;
                case R.id.btn_right:
                    if (!isEdit)
                        startActivity(new Intent(MainActivity.this,ShuChengActivity.class));
                    else {
                        adpter.setSelectAll(isAll=!isAll);
                        setRight_Btn(isAll ? "取消" : "全选",0);
                    }
                    break;
                case R.id.search_novel:
                    startActivity(new Intent(MainActivity.this, StoryActivity.class));
                    break;
                case R.id.delete:
                    doDelete();
                    break;
                case R.id.sign_in:
                    sin_in();
                    break;
            }
        }
    };

    private CheckedListener listener = new CheckedListener() {
        @Override
        public void changedNum(String num) {
            delete.setText("删除（"+num+"）");
        }
    };

    public interface CheckedListener{
        public void changedNum(String num);
    }
}
